package com.example.leetcode.prcatice;

import java.util.HashSet;
import java.util.Set;

/**
 * 剑指 Offer 52. 两个链表的第一个公共节点
 */
public class Offer52 {

    public ListNode2 getIntersectionNode(ListNode2 headA, ListNode2 headB) {
        Set<ListNode2> set = new HashSet<>();
        ListNode2 temp = headA;
        while (temp != null){
            set.add(temp);
            temp = temp.next;
        }
        temp = headB;
        while (temp != null){
            if(set.contains(temp)){
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }
}


class ListNode2 {
      int val;
      ListNode2 next;
      ListNode2(int x) {
         val = x;
        next = null;
      }
  }